<template>
  <div class="container">
    <van-search
      v-model="searchform.strUsername"
      show-action
      placeholder="请输入名称"
      @search="onSearch"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <div class="list-container">
      <div class="section">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            :finished-text="finishedText"
            @load="fetchData"
          >
            <p v-if="listData.length===0" class="yyp">
              <van-empty description="暂无数据~"/>
            </p>
            <van-swipe-cell v-else v-for="item in listData" :key="item.id">
              <div class="yycard" @click="wxShowDetail(item)">
                <div class="yycard_p">
                  <p class="yycard_title">{{item.strUsername}}</p>
                  <p>所在机构：{{item.strOrgname}}</p>
                  <p>创建者：{{item.strOperatorname}}</p>
                </div>
              </div>
              <template #right>
                <van-button square @click="wxdeleteInfo(item,$event)" text="删除" type="danger" class="delete-button"/>
                <!--<van-button @click="wxShowDetail(item)" square text="详情" type="danger" class="delete-button2"/>-->
              </template>
            </van-swipe-cell>
          </van-list>
        </van-pull-refresh>
      </div>
      <div class="bottom">
        <van-button type="primary" block @click="addProgramme">添加</van-button>
      </div>
    </div>
  </div>
</template>

<script>
  import {getList, deleteInfo} from '../../api/programme'
  import {Dialog} from "vant";

  export default {
    name: 'programme',
    data() {
      return {
        list: [],
        listData: [],
        loading: false,
        finished: false,
        finishedText: '',
        refreshing: '',
        pagenum: 1,
        localStorageform: {
          //pagenum: 1,
          userOrg: localStorage.getItem('iOrgsysnum')
        },
        searchform: {
          strUsername: ''
        }
      }
    },
    created() {
      /*this.fetchData()*/
    },

    methods: {
      //获取节目单列表
      /*fetchData() {
        getList(this.searchform).then(res => {
          this.listData = res.pages
        })
      },*/

      //根据节目单名称搜索
      onSearch() {
        this.pagenum = 1
        this.listData = []
        this.fetchData()
      },

      //节目单详情
      wxShowDetail(item) {
        this.$router.push({
          path: 'detailProgramme',
          query: {rowAll: JSON.stringify(item)}
        })
      },

      //删除节目单
      wxdeleteInfo(item, event) {
        event.stopPropagation()
        let param = {
          iOrgsysnum: item.iOrgsysnum,
          iProgramsysnum: item.iProgramsysnum
        }
        deleteInfo(param)
        Dialog.confirm({
          message: '确定删除该数据?'
        }).then(res => {
          console.log(res)
          this.$toast.success('删除成功')
          setTimeout(() => {
            this.pagenum = 1
            this.listData = []
            this.fetchData()
          }, 1000)
        })
      },
      onLoad() {
        // 异步更新数据
        // setTimeout 仅做示例，真实场景中一般为 ajax 请求
        setTimeout(() => {
          if (this.refreshing) {
            this.listData = [];
            this.refreshing = false;
          }
          getList({userOrg: this.localStorageform.userOrg, pagenum: 1}).then(res => {
            this.listData = res && res.pages
          })
          // 加载状态结束
          this.loading = false;
          // 数据全部加载完成
          if (this.listData.length >= 5) {
            this.finished = true
          }
        }, 1000);
      },
      async fetchData() {
        this.loading = true
        let param = {
          userOrg: this.localStorageform.userOrg,
          pagenum: this.pagenum,
          strUsername: this.searchform.strUsername
        }
        let ret = await getList(param)
        this.loading = false
        if (ret && ret.pages) {
          const {currentpage, pages, pagenum, total} = ret
          let list = pages || []
          if (pagenum > 1) {
            this.listData = [...this.listData, ...list]
          } else {
            this.listData = list
          }
          if (total % 5 === 0) {
            this.pages = total / 5
          } else {
            this.pages = parseInt(total / 5) + 1
          }
          console.log(this.pages)
          // 如果当前页数 = 总页数，则已经没有数据
          if (currentpage === this.pages) {
            this.finished = true
            this.finishedText = '- 没有更多了-'
          }
          // 如果总页数大于当前页码，页码+1
          if (this.pages > currentpage) {
            this.pagenum++
          }
        }
      },
      //下拉刷新
      onRefresh() {
        // 清空列表数据
        this.finished = false;

        // 重新加载数据
        // 将 loading 设置为 true，表示处于加载状态
        this.loading = true;
        this.onLoad();
      },

      //跳转到新增页面
      addProgramme() {
        this.$router.push({
          path: 'addProgramme',
          query: {buttonadd: JSON.stringify(1)}
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .container {
    height: 100%;

    .list-container {
      display: flex;
      flex-direction: column;

      .section {
        flex: 1;
        height: 480px;
        max-height: 480px;
        overflow: auto;
        padding-bottom: 100px;
      }

      .bottom {
        flex: 1;
        display: flex;
        justify-content: center;
        align-content: center;
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        background: #fafafa;

        .van-button {
          width: 90%;
        }
      }
    }
  }

  .yyp {
    text-align: center;
    font-size: 14px;
    color: #8c8a8a;
    line-height: 2;
  }

  .yycard {
    display: flex;
    border-bottom: 1px solid #f2f3f5;
    padding: 5px 0;
    margin-left: 20px;
  }

  /deep/ .van-image__img {
    border-radius: 8px;
  }

  .yycard_p {
    font-size: 12px;
    line-height: 20px;
    vertical-align: middle;
    padding-left: 10px;
    color: #6b6b6b;
  }

  .yycard_title {
    font-size: 14px;
    color: #000;
  }

  .van-button--primary {
    background-color: #0c1b35;
    border-color: #0c1b35;
    border-radius: 4px;
    width: 90%;
    margin: 20px;
  }

  .van-button--danger {
    height: 100%;
  }

  .delete-button2 {
    background-color: #6b6b6b;
    border: 1px solid #6b6b6b;
  }

  /deep/ .van-search__content {
    border-radius: 20px;
    background-color: #e7e9ec;
  }


</style>
